﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class UIZABED : ViewBase
    {
        public VZABED TMV
        {
            get
            {
                return (VZABED)MV;
            }
        }
        #region ==dwZA===========
        public static readonly DependencyProperty dwZAProperty = DependencyProperty.Register("dwZA",
            typeof(DataRow), typeof(UIZABED),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwZAChanged));
        public DataRow dwZA
        {
            get
            {
                return (DataRow)GetValue(dwZAProperty);
            }
            set
            {
                SetValue(dwZAProperty, value);
            }
        }
        private static void OdwZAChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIZABED;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwZA = row;
            }
        }
        #endregion
        
        #region ==SKONP1===========
        public static readonly DependencyProperty SKONP1Property = DependencyProperty.Register("SKONP1",
        typeof(double), typeof(UIZABED),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSKONP1Changed));
        public double SKONP1
        {
            get
            {
                return (double)GetValue(SKONP1Property);
            }
            set
            {
                SetValue(SKONP1Property, value);
            }
        }
        private static void OnSKONP1Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIZABED;
            if (src != null)
            {
                src.SKONP1 = (double)e.NewValue;
             }
        }
        #endregion
        public UIZABED()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
        }
        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwZA = dwMaster;
            if (dwMaster != null)
            {
                KEY = TMV.ZABNR;
                SKONP1 = TMV.ZABSKONP1;
                TXTISIND = (TMV.ZABNR + "V").PadRight(60);
                INFO = TMV.ZABNR+":"+Helper.GetGB2312Value( TMV.ZABBEZ.Trim())+",贴息:"+ SKONP1/1000+"%";
            }
            else
            {
                SKONP1 = 0;
                KEY = "";
                TXTISIND = "";
                INFO = "";
            }
        }
        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (IsManage)
            {
                LOADALL();
                Locate("KEY", ORDERNUM);
            }
            else
            {
                VICC_WFEVENTS.SVWF.ZA02(this.RUNID);
            }
            base.ManageCommand(sender, e);
        }
        public void ZALOAD(MessageInfo msg)
        {
            if (msg.P1.Trim().Length == 0)
            {
                TBMain.Clear();
                return;
            }
            dwData = TBMain.NewRow();
            dwData["ZABMANDANT"] = app.Mandant;
            dwData["ZABNR"] = msg.P1;
            SResult = ((VZABED)MV).QueryZABED(dwData, 1);
            MergeData(SResult, true);
        }

        public void Insert(DataRow _dwZA)
        {
            try
            {
                ((VZABED)MV).InsertZABED(_dwZA, app.company);
                TBMain.Rows.InsertAt(_dwZA, 0);
                vwMaster.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            foreach(DataRow  row in TBMain.Rows)
            {
                if (row.RowState == DataRowState.Unchanged) continue;
                TMV.UpdateZABED(row, app.company, 0);
            } 
            base.OnSave();
        }
        public void LSDEL(MessageInfo msg)
        {
            dwData = TBMain.NewRow();
            dwData["ZABMANDANT"] = app.Mandant;
            dwData["ZABNR"] = msg.P1;
            ((VZABED)MV).DeleteZABED(dwData, 1);
        }
    }
}
